﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using DOAN.DO;
using DOAN.BL;

namespace DOAN.Admin
{
    public partial class QL_Dethi : System.Web.UI.Page
    {
        string cauhoi = "";
        string ht = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["quyen"] == null)
                Response.Redirect("LoginAdmin.aspx");
            else
            {
                if ((Session["quyen"].ToString() == "AD") || (Session["quyen"].ToString() == "GV"))
                {
                    if (Session["Tendangnhap"] != null && Session["Tendangnhap"].ToString() != "")
                    {
                        MasterPage mt = this.Master as MasterPage;
                        LinkButton lb = mt.FindControl("LinkButton1") as LinkButton;
                        lb.Visible = false;
                        Label bl = mt.FindControl("lblDnthanhcong") as Label;
                        bl.Visible = true;
                        Label blten = mt.FindControl("lbtendn") as Label;
                        blten.Visible = true;
                        blten.Text = Session["Tendangnhap"].ToString();
                        LinkButton lbDangky = mt.FindControl("lbDangky") as LinkButton;
                        lbDangky.Visible = false;
                        LinkButton lbthoat = mt.FindControl("lbThoat") as LinkButton;
                        lbthoat.Visible = true;
                        Session["MaTK"].ToString();
                    }

                    if (!IsPostBack)
                    {
                        string tentaikhoan = Session["Tendangnhap"].ToString();
                        loadMon();
                    }
                }
                else
                {
                    Response.Redirect("~/Default.aspx");
                }
            }
        }
        public void loadMon()
        {
            DataTable dt = new DataTable();
            if (Session["quyen"].ToString() == "GV")
                dt = new CT_MonhocBL().GetByUser(Session["Tendangnhap"].ToString());
            else if (Session["quyen"].ToString() == "AD")
                dt = new MonhocBL().SelectAll();
            if (dt.Rows.Count > 0)
            {
                drpMon.DataSource = dt;
                drpMon.DataTextField = "Tenmon";
                drpMon.DataValueField = "Mamonhoc";
                drpMon.DataBind();
            }
            else
            {
                drpMon.Text = "Khong co mon";
            }
        }
        public void load()
        {

            int i = int.Parse((string)(new MonhocBL().GetSoChuong(drpMon.SelectedValue)));

            for (int j = 1; j <= i; j++)
            {
                //Label lbl = (Label)rpt.FindControl("lblChuong");
                //lbl.Text = "So cau chuong " + j;
            }
        }
        private void DS_cauhoi_DK(string mon, int kho, int tb, int de)
        {
            try
            {
                GridDethi.DataSource = new CauhoiBL().TaoDe(mon, kho, tb, de);
                GridDethi.DataBind();

            }
            catch (Exception ee)
            {

            }
            finally
            {
            }
        }
        protected void btTao_Click(object sender, EventArgs e)
        {
            if (int.Parse(txtTong.Text) != (int.Parse(txtDe.Text) + int.Parse(txtKho.Text) + int.Parse(txtTB.Text)))
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('Không đủ số câu tạo đề!');", true);
            }
            else
            {
                GridDe.Visible = true;
                GridDe2.Visible = false;
                GridDe3.Visible = false;
                //DS_cauhoi_DK(drpMon.SelectedValue, int.Parse(txtKho.Text), int.Parse(txtTB.Text), int.Parse(txtDe.Text));
                DataTable dt = new CauhoiBL().TaoDe(drpMon.SelectedValue, int.Parse(txtKho.Text), int.Parse(txtTB.Text), int.Parse(txtDe.Text));
                int count = GridDethi.Rows.Count;

                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    if (j == 0)
                        //ar[j] = dethi.Rows[j].ItemArray[0].ToString();
                        cauhoi = dt.Rows[j].ItemArray[0].ToString();
                    else
                        //ar[j] = "-" + dethi.Rows[j].ItemArray[0].ToString();
                        cauhoi += "-" + dt.Rows[j].ItemArray[0].ToString();
                }
                if (dt.Rows.Count < int.Parse(txtTong.Text))
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('Không đủ số câu tạo đề!');", true);
                else
                {
                    Session["DSCH"] = cauhoi;
                    GridDethi.Columns[0].Visible = false;
                    //GridDethi.DataSource = dt;
                    //GridDethi.DataBind();
                    DS_cauhoi_DK(drpMon.SelectedValue, int.Parse(txtKho.Text), int.Parse(txtTB.Text), int.Parse(txtDe.Text));
                    btLuu.Visible = true;
                    Button2.Visible = false;
                }
            }
        }
        protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                int i = int.Parse((string)(new MonhocBL().GetSoChuong(drpMon.SelectedValue)));

                for (int j = 1; j <= i; j++)
                {
                    ((Label)e.Item.FindControl("lblChuong")).Text = "So cau chuong " + j;
                }
            }
        }
        protected void btLuu_Click(object sender, EventArgs e)
        {
            //foreach (DataRow dr in GridDethi.Rows)
            //{
            //    if (cauhoi.Count == 0)
            //        cauhoi.Add(dr[0].ToString());
            //    else
            //    {
            //        string s = "-" + dr[0].ToString();
            //        cauhoi.Add(s);
            //    }
            //}



            //for (int i = 0; i < count; i++)
            //{
            //    GridViewRow row = GridDethi.Rows[i];
            //    if (i == 0)
            //    {
            //        ar[i] = ((DataBoundLiteralControl)row.Cells[0].Controls[0]).Text;
            //        cauhoi.Add(row.Cells[0].ToString());
            //    }
            //    else
            //    {
            //        string s = "-" + ((DataBoundLiteralControl)row.Cells[0].Controls[0]).Text;
            //        ar[i] = s;
            //        cauhoi.Add(s);
            //    }
            //}
            DethiDO obj = new DethiDO();
            obj.Mamonhoc = drpMon.SelectedValue;
            obj.Ngaytao = DateTime.Now.ToString();
            obj.Socau = int.Parse(txtTong.Text);
            obj.Sodiem = 100;
            obj.Tentaikhoan = Session["Tendangnhap"].ToString();
            obj.Thoigian = 15;
            obj.Tieude = "TieuDe";
            //obj.Trangthai =bool.Parse(drTT.SelectedValue.ToString());
            obj.Trangthai = true;
            obj.Macauhoi = Session["DSCH"].ToString();
            new DethiBL().Insert(obj);
        }
        private void DS_cauhoi_TC(string mon)
        {
            DataTable dt = new CauhoiBL().GetByMon(mon);
            try
            {
                GridDethi2.DataSource = dt;
                GridDethi2.DataBind();
                lbSumrecord.Text = dt.Rows.Count.ToString() ;
            }
            catch (Exception ee)
            {

            }
            finally
            {
                dt.Dispose();
            }
        }
        protected void rdTD_CheckedChanged(object sender, EventArgs e)
        {
            if (rdTD.Checked == true)
            {
                rdDokho.Checked = false;
                RadioButton2.Checked = false;
                btLuu.Visible = true;
                pnTaoDeTD.Visible = true;
                GridDe.Visible = false;
                GridDe2.Visible = false;
                GridDe3.Visible = false;
            }
            else
            {
                pnTaoDeTD.Visible = false;
                GridDe.Visible = true;
                btLuu.Visible = false;
                GridDethi.Columns[0].Visible = true;
                DataTable dt = new DataTable();
                dt = new CauhoiBL().GetByMon(drpMon.SelectedValue);
                if (dt.Rows.Count > 0)
                {
                    GridDethi.DataSource = dt;
                    GridDethi.DataBind();
                }
                else
                {
                    GridDethi.DataBind();
                    GridDethi.Attributes.Add("EmptyDataText", "Khong co du lieu");
                }
            }

        }
        protected void rdTC_CheckedChanged(object sender, EventArgs e)
        {
            if (rdTC.Checked == false)
            {
                pnTaoDeTD.Visible = true;
                GridDe.Visible = false;

            }
            else
            {
                pnTaoDeTD.Visible = false;
                GridDe2.Visible = true;
                btLuu.Visible = false;
                Button3.Visible = false;
                pnDokho.Visible = false;
                pnChuong.Visible = false;
                Button2.Visible = true;
                GridDethi2.Columns[0].Visible = true;
                DS_cauhoi_TC(drpMon.SelectedValue);
                /*
                DataTable dt = new DataTable();
                dt = new CauhoiBL().GetByMon(drpMon.SelectedValue);

                if (dt.Rows.Count > 0)
                {
                    GridDethi.DataSource = dt;
                    GridDethi.DataBind();
                }
                else
                {
                    GridDethi.DataBind();
                    GridDethi.Attributes.Add("EmptyDataText", "Khong co du lieu");
                }*/

            }
        }
        protected void btLuu2_Click(object sender, EventArgs e)
        {
            //foreach (GridViewRow row in GridDethi.Rows)
            //{
            //    CheckBox check = (CheckBox)row.FindControl("ckb");
            //    if (check != null && check.Checked)
            //    {
            //        if (row.RowIndex == 0)
            //            cauhoi = row;
            //        else
            //            //ar[j] = "-" + dethi.Rows[j].ItemArray[0].ToString();
            //            cauhoi += "-" + row.Cells[0].Text;
            //    }
            //}
            int tong = 0;
            ArrayList userdetails = (ArrayList)Session["CHECKED_ITEMS"];
            if (userdetails != null && userdetails.Count > 0)
            {
                for (int ktt = 0; ktt < userdetails.Count; ktt++)
                    cauhoi += userdetails[ktt] + "-";
            }
            for (int i = 0; i < GridDethi2.Rows.Count; i++)
            {
                CheckBox check = (CheckBox)GridDethi2.Rows[i].FindControl("ckb2");
                if (check != null && check.Checked)
                {
                    cauhoi += ((Label)GridDethi2.Rows[i].FindControl("lblMaCH2")).Text + "-";
                    tong++;
                }
            }
            cauhoi.Substring(0, cauhoi.Length - 1);
            DethiDO obj = new DethiDO();
            obj.Mamonhoc = drpMon.SelectedValue;
            obj.Ngaytao = DateTime.Now.ToString();
            obj.Socau = tong;
            obj.Sodiem = 100;
            obj.Tentaikhoan = Session["Tendangnhap"].ToString();
            obj.Thoigian = 15;
            obj.Tieude = "TieuDe";
            obj.Trangthai = true;
            obj.Macauhoi = cauhoi;
            new DethiBL().Insert(obj);

        }
        protected void GridDethi_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //DataRowView obj = e.Row.DataItem;
            //CheckBox cbCheck = e.Row.FindControl("ckb");
            //if (cbCheck.Checked == true)
            //{
            //}
        }
        protected void GridDethi2_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //DataRowView obj = e.Row.DataItem;
            //CheckBox cbCheck = e.Row.FindControl("ckb");
            //if (cbCheck.Checked == true)
            //{
            //}
        }
        protected void GridDethi3_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //DataRowView obj = e.Row.DataItem;
            //CheckBox cbCheck = e.Row.FindControl("ckb");
            //if (cbCheck.Checked == true)
            //{
            //}
        }
        protected void rdDokho_CheckedChanged(object sender, EventArgs e)
        {
            pnDokho.Visible = true;
            pnChuong.Visible = false;
        }
        protected void rdChuong_CheckedChanged(object sender, EventArgs e)
        {
            pnDokho.Visible = false;
            pnChuong.Visible = true;
        }
        private void DS_cauhoi_Chuong(string mon, int c1, int c2, int c3, int c4, int c5, int c6, int c7, int c8, int c9, int c10)
        {
            try
            {
                GridDethi3.DataSource = new CauhoiBL().TaoDe2(mon, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10);
                GridDethi3.DataBind();

            }
            catch (Exception ee)
            {

            }
            finally
            {
            }
        }
        protected void btTao2_Click(object sender, EventArgs e)
        {
            if (int.Parse(txttong2.Text) != (int.Parse(txtChuong1.Text) + int.Parse(txtChuong2.Text) + int.Parse(txtChuong3.Text) + int.Parse(txtChuong4.Text) + int.Parse(txtChuong5.Text) + int.Parse(txtChuong6.Text) + int.Parse(txtChuong7.Text) + int.Parse(txtChuong8.Text) + int.Parse(txtChuong9.Text) + int.Parse(txtChuong10.Text)))
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('Không đủ số câu tạo đề!');", true);
            }
            else
            {
                GridDe.Visible = false;
                GridDe3.Visible = true;

                DataTable dt = new CauhoiBL().TaoDe2(drpMon.SelectedValue, int.Parse(txtChuong1.Text), int.Parse(txtChuong2.Text), int.Parse(txtChuong3.Text), int.Parse(txtChuong4.Text), int.Parse(txtChuong5.Text), int.Parse(txtChuong6.Text), int.Parse(txtChuong7.Text), int.Parse(txtChuong8.Text), int.Parse(txtChuong9.Text), int.Parse(txtChuong10.Text));
                int count = GridDethi3.Rows.Count;

                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    if (j == 0)
                        //ar[j] = dethi.Rows[j].ItemArray[0].ToString();
                        cauhoi = dt.Rows[j].ItemArray[0].ToString();
                    else
                        //ar[j] = "-" + dethi.Rows[j].ItemArray[0].ToString();
                        cauhoi += "-" + dt.Rows[j].ItemArray[0].ToString();
                }
                if (dt.Rows.Count < int.Parse(txttong2.Text))
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('Không đủ số câu tạo đề!');", true);
                else
                {
                    Session["DSCH"] = cauhoi;
                    GridDethi3.Columns[0].Visible = false;
                    DS_cauhoi_Chuong(drpMon.SelectedValue, int.Parse(txtChuong1.Text), int.Parse(txtChuong2.Text), int.Parse(txtChuong3.Text), int.Parse(txtChuong4.Text), int.Parse(txtChuong5.Text), int.Parse(txtChuong6.Text), int.Parse(txtChuong7.Text), int.Parse(txtChuong8.Text), int.Parse(txtChuong9.Text), int.Parse(txtChuong10.Text));
                    //GridDethi.DataSource = dt;
                    //GridDethi.DataBind();
                    btLuu.Visible = false;
                    Button3.Visible = true;
                    Button2.Visible = false;
                }
            }
        }
        protected void btLuu3_Click(object sender, EventArgs e)
        {
            DethiDO obj = new DethiDO();
            obj.Mamonhoc = drpMon.SelectedValue;
            obj.Ngaytao = DateTime.Now.ToString();
            obj.Socau = int.Parse(txttong2.Text);
            obj.Sodiem = 100;
            obj.Tentaikhoan = Session["Tendangnhap"].ToString();
            obj.Thoigian = 15;
            obj.Tieude = "TieuDe";
            //obj.Trangthai =bool.Parse(drTT.SelectedValue.ToString());
            obj.Trangthai = true;
            obj.Macauhoi = Session["DSCH"].ToString();
            new DethiBL().Insert(obj);
        }

        protected void gridCauhoi_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            //string monhoc = drMonhoc1.SelectedValue;
            GridDethi.PageIndex = e.NewPageIndex;
            //this.DS_cauhoi_TC(drpMon.SelectedValue);
            this.DS_cauhoi_DK(drpMon.SelectedValue, int.Parse(txtKho.Text), int.Parse(txtTB.Text), int.Parse(txtDe.Text));
            //this.DS_cauhoi_Chuong(drpMon.SelectedValue, int.Parse(txtChuong1.Text), int.Parse(txtChuong2.Text), int.Parse(txtChuong3.Text), int.Parse(txtChuong4.Text), int.Parse(txtChuong5.Text), int.Parse(txtChuong6.Text), int.Parse(txtChuong7.Text), int.Parse(txtChuong8.Text), int.Parse(txtChuong9.Text), int.Parse(txtChuong10.Text));


        }

        protected void gridCauhoi3_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridDethi3.PageIndex = e.NewPageIndex;
            this.DS_cauhoi_Chuong(drpMon.SelectedValue, int.Parse(txtChuong1.Text), int.Parse(txtChuong2.Text), int.Parse(txtChuong3.Text), int.Parse(txtChuong4.Text), int.Parse(txtChuong5.Text), int.Parse(txtChuong6.Text), int.Parse(txtChuong7.Text), int.Parse(txtChuong8.Text), int.Parse(txtChuong9.Text), int.Parse(txtChuong10.Text));
        }

        private void PopulateCheckedValues()
        {
            ArrayList userdetails = (ArrayList)Session["CHECKED_ITEMS"];
            if (userdetails != null && userdetails.Count > 0)
            {
                foreach (GridViewRow gvrow in GridDethi2.Rows)
                {
                    int index = (int)GridDethi2.DataKeys[gvrow.RowIndex].Value;
                    if (userdetails.Contains(index))
                    {
                        CheckBox myCheckBox = (CheckBox)gvrow.FindControl("ckb2");
                        myCheckBox.Checked = true;
                        ht += ((Label)gvrow.FindControl("lblMaCH2")).Text + "-";
                    }
                }
            }
        }
        //This method is used to save the checkedstate of values
        private void SaveCheckedValues()
        {
            ArrayList userdetails = new ArrayList();
            int index = -1;
            foreach (GridViewRow gvrow in GridDethi2.Rows)
            {
                index = (int)GridDethi2.DataKeys[gvrow.RowIndex].Value;
                bool result = ((CheckBox)gvrow.FindControl("ckb2")).Checked;

                // Check in the Session
                if (Session["CHECKED_ITEMS"] != null)
                    userdetails = (ArrayList)Session["CHECKED_ITEMS"];
                if (result)
                {
                    if (!userdetails.Contains(index))
                        userdetails.Add(index);
                }
                else
                    userdetails.Remove(index);
            }
            if (userdetails != null && userdetails.Count > 0)
                Session["CHECKED_ITEMS"] = userdetails;
        }
        protected void gridCauhoi2_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            SaveCheckedValues();
            GridDethi2.PageIndex = e.NewPageIndex;
            this.DS_cauhoi_TC(drpMon.SelectedValue);
            PopulateCheckedValues();
        }
        //protected void GridDethi2_PreRender(object sender, EventArgs e)
        //{
        //    if (Session["page" + GridDethi2.PageIndex] != null)
        //    {
        //        CheckBox chb;
        //        bool[] values = (bool[])Session["page" + GridDethi2.PageIndex];
        //        for (int i = 0; i < GridDethi2.Rows.Count; i++)
        //        {
        //            chb = (CheckBox)GridDethi2.Rows[i * GridDethi2.PageIndex].FindControl("ckb2");
        //            chb.Checked = values[i];
        //        }
        //    }
        //}
    }
}
